package com.ineatconseil.yougo.web.controllers;

import java.util.ArrayList;
import java.util.List;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.ineatconseil.yougo.bo.UserType;
import com.ineatconseil.yougo.dao.UserTypeDao;
import com.ineatconseil.yougo.dto.UserTypeDto;
import com.ineatconseil.yougo.web.exceptions.InternalServerErrorException;
import com.ineatconseil.yougo.web.exceptions.NotFoundException;

@Controller
@RequestMapping("/user-types")
public class UserTypeController {

	@Autowired
	private UserTypeDao dao;

	@Autowired
	private Mapper dozerMapper;

	@RequestMapping(method = RequestMethod.GET, value = "{id}")
	public @ResponseBody UserTypeDto find(@PathVariable Long id) {
		try {
			return dozerMapper.map(dao.get(id), UserTypeDto.class);
		} catch (ObjectRetrievalFailureException e) {
			throw new NotFoundException();
		} catch (Exception e) {
			throw new InternalServerErrorException();
		}
	}

	@RequestMapping(method = RequestMethod.GET)
	public @ResponseBody List<UserTypeDto> findAll() {
		try {
			List<UserType> userTypes = dao.findAll();
			List<UserTypeDto> userTypeDtos = new ArrayList<UserTypeDto>(userTypes.size());
			for (UserType userType : userTypes) {
				userTypeDtos.add(dozerMapper.map(userType, UserTypeDto.class));
			}
			return userTypeDtos;
		} catch (Exception e) {
			throw new InternalServerErrorException();
		}
	}

	@RequestMapping(method = RequestMethod.POST)
	@ResponseStatus(HttpStatus.CREATED)
	public void save(@RequestBody UserTypeDto userTypeDto) {
		// TODO lancer une BadRequestException si problème client, Spring lance des UnrecognizedPropertyException mais impossible de les chopper
		// TODO Empecher l'attribution de l'id
		// TODO Renvoyer le Response header location avec l'URI du nouveau user créé
		try {
			dao.save(dozerMapper.map(userTypeDto, UserType.class));
		} catch (Exception e) {
			throw new InternalServerErrorException();
		}
	}

	@RequestMapping(method = RequestMethod.PUT, value = "{id}")
	@ResponseStatus(HttpStatus.OK)
	public void update(@PathVariable Long id, @RequestBody UserTypeDto userTypeDto) {
		// TODO lancer une BadRequestException si problème client, Spring lance des UnrecognizedPropertyException mais impossible de les chopper
		// TODO Vérifier que l'id est cohérent avec la requete
		try {
			UserType userType = dao.get(id);
			dozerMapper.map(userTypeDto, userType);
			dao.save(userType);
		} catch (ObjectRetrievalFailureException e) {
			throw new NotFoundException();
		} catch (Exception e) {
			throw new InternalServerErrorException();
		}
	}

	@RequestMapping(method = RequestMethod.DELETE, value = "{id}")
	@ResponseStatus(HttpStatus.OK)
	public void delete(@PathVariable Long id) {
		try {
			dao.remove(id);
		} catch (ObjectRetrievalFailureException e) {
			throw new NotFoundException();
		} catch (Exception e) {
			throw new InternalServerErrorException();
		}
	}

}
